// CIS 3100 Last Working Assignment in C++

//Thank You Professor I'll see you in 3400

// array of structures
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>


using namespace std;

struct LoanInfoRequest {
  double amount;
  double rate;
  int years;
};
struct LoanInfoResponse {
  double principal;
  double amount;
  string paymentDescription;
  int returnCode;
};

struct HomeEquityRequest {
  double income;
  int incomeSources;
  double monthlyDebtPayments;
  double creditCardLimit;
  int dependents;
  double realestateAmountToSecureLoan;
};
struct HomeEquityResponse {
  string loanApproval;
  string primaryResponse;
  string descriptionResponse;
  int returnCode;
  
};
double getPrincipal(double amount,double rate,int years)
{

	double lPrincipal = 0;

	lPrincipal = (amount * pow((1+((rate/100)/2)),years))/(years*12);
	
	return lPrincipal;
}


LoanInfoResponse getLoanInformation(LoanInfoRequest request)
{
	string l_principal;
	string l_paymentAmount;

	stringstream outPrincipal;
	stringstream outMonthPayment;

	outPrincipal << request.amount;
	l_principal = outPrincipal.str();
	
	LoanInfoResponse response;
	double l_response = 0.0;

	response.amount = getPrincipal(request.amount,request.rate,request.years);
	
	outMonthPayment << response.amount;
	l_paymentAmount = outMonthPayment.str();

	response.principal = request.amount;
	l_response = request.amount;
	response.paymentDescription = "Your $" + l_principal + " Loan has a monthly payment of $" +  l_paymentAmount;

	response.returnCode = 0;
	
	return response;
}


HomeEquityResponse getHomeEquityLoan(LoanInfoRequest request,HomeEquityRequest homeEquityRequest)
{

	HomeEquityResponse response;
	double percentageOfPrinicpal = 3;
	double monthlyDebtThreshold = 0.0;
	bool incomeRequirementSuccessful = false;

	double point = 0;

	if((request.amount /(homeEquityRequest.income)) <= percentageOfPrinicpal)
	{
		point = 1;
	}
	if(homeEquityRequest.incomeSources <= 2)
	{
		point = point + .5;
	}
	monthlyDebtThreshold = (homeEquityRequest.income)-(homeEquityRequest.monthlyDebtPayments*12);

	if((monthlyDebtThreshold - (getPrincipal(request.amount,request.rate,request.years)*12)) > (homeEquityRequest.creditCardLimit*.50))
	{
		point = point + 1;
		incomeRequirementSuccessful = true;
	}

	if(homeEquityRequest.dependents <=2 || incomeRequirementSuccessful == true)
	{
		point = point + .5;
	}
	if((homeEquityRequest.realestateAmountToSecureLoan/request.amount) > 1)
	{
		point = point + 2;
	}
	else
	{
		point = 0;
	}

	if(point < 2) {

		response.descriptionResponse = "Loan was not approved. Please consult with a financial advisor for more details.";
		response.loanApproval = "Unlikely";
		response.returnCode = 0;
		response.primaryResponse = "Loan was not approved";	
	}

	if(point > 2 && point < 3.5) {

		response.descriptionResponse = "Loan approved with conditions. Please consult with a financial advisor for more details.";
		response.loanApproval = "Fair";
		response.returnCode = 1;
		response.primaryResponse = "Loan was approved with conditions";	
	}

	if(point > 3.5) {

		response.descriptionResponse = "Loan approved with no conditions. Please financial advisor for more details.";
		response.loanApproval = "Lilely";
		response.returnCode = 1;
		response.primaryResponse = "Loan was approved with no conditions.";	
	}

	return response;
}

int HomeEquityApplication()
{
    cout << "Loan prequalification calculator 4.5 Release 2.0";
	cout << "Bunis Walker (Kalel) HomeEquity 4000 Calculator\n\n";
		
    cout << "Step 1 - Enter New Loan Information for Estimate Monthly Payment\n\n";
	
	LoanInfoRequest testLoanInfoRequest;

	cout << "Enter Requested Principal: ";
	cin >>  testLoanInfoRequest.amount;

	cout << "Enter Interest Rate: ";
	cin >>  testLoanInfoRequest.rate;

	cout << "Enter Years of Loan: ";
	cin >>  testLoanInfoRequest.years;

	cout << "Your Monthly will be: " << getLoanInformation(testLoanInfoRequest).paymentDescription << "\n\n";

	cout << "Step 2 Enter Your Information to check Prequalification \n\n";

	//Test Data for Test 3

	HomeEquityRequest testHomeEquityRequest;


	cout << "Enter Your Annual Income: ";
	cin >>  testHomeEquityRequest.income;

	cout << "Enter Number of Dependents: ";
	cin >>  testHomeEquityRequest.dependents;

	cout << "Enter Your Credit Card Limit: ";
	cin >>  testHomeEquityRequest.creditCardLimit;

	cout << "Enter Number of Income Sources: ";
	cin >>  testHomeEquityRequest.incomeSources;

	cout << "Enter Your Total Monthly Debt: ";
	cin >>  testHomeEquityRequest.monthlyDebtPayments;

	cout << "Enter Your Realstate Amount to Secure Loan (should be your home value): ";
	cin >>  testHomeEquityRequest.realestateAmountToSecureLoan;
	
	//Test Two
	//cout << "Amount: " << getLoanInformation(testLoanInfoRequest).amount << "\n";
	//cout << "Description: " << getLoanInformation(testLoanInfoRequest).paymentDescription << "\n";
	//cout << "Principal: " << getLoanInformation(testLoanInfoRequest).principal << "\n";
	//cout << "Unit Test MonthlyPayment ReturnCode: " << getLoanInformation(testLoanInfoRequest).returnCode << "\n";

	//Test Three
	cout << "\n\nDescription: " << getHomeEquityLoan(testLoanInfoRequest,testHomeEquityRequest).descriptionResponse << "\n";
	//cout << "MonthlyPayment Loan Approval: " << getHomeEquityLoan(testLoanInfoRequest,testHomeEquityRequest).loanApproval << "\n";
	//cout << "Response: " << getHomeEquityLoan(testLoanInfoRequest,testHomeEquityRequest).primaryResponse << "\n";
	//cout << "Unit Test MonthlyPayment ReturnCode: " << getHomeEquityLoan(testLoanInfoRequest,testHome//EquityRequest).returnCode << "\n";
		
	system("PAUSE");
	return 0;
	//Give me but one firm spot on which to stand, and I will move the earth
   //- Archimedes
   
}

